package shg.book.control
{
	
	import flash.events.MouseEvent;
	import flash.text.engine.FontPosture;
	import flash.text.engine.FontWeight;
	import flash.text.engine.TextRotation;
	import flash.xml.XMLDocument;
	import flash.xml.XMLNode;
	
	import flashx.textLayout.edit.EditManager;
	import flashx.textLayout.edit.ElementRange;
	import flashx.textLayout.edit.IEditManager;
	import flashx.textLayout.elements.TextFlow;
	import flashx.textLayout.events.SelectionEvent;
	import flashx.textLayout.formats.FormatValue;
	import flashx.textLayout.formats.LeadingModel;
	import flashx.textLayout.formats.TextAlign;
	import flashx.textLayout.formats.TextDecoration;
	import flashx.textLayout.formats.TextJustify;
	import flashx.textLayout.formats.TextLayoutFormat;
	
	import mx.collections.ArrayCollection;
	import mx.collections.XMLListCollection;
	import mx.controls.ColorPicker;
	import mx.controls.Label;
	import mx.events.CollectionEvent;
	import mx.events.ColorPickerEvent;
	import mx.events.DropdownEvent;
	import mx.events.PropertyChangeEvent;
	import mx.managers.PopUpManager;
	
	
	import spark.components.RichEditableText;
	import spark.utils.TextFlowUtil;

	public class Author_Edit
	{
		private var textFlow:TextFlow;
		
		[Bindable]
		private var colorValue:int=0x000000;
		[Bindable]
		private var fontSizeValue:int=12;
		[Bindable]
		private var fontFamilyValue:String;
		[Bindable]
		private var fontWeight:String=FontWeight.NORMAL;
		
		[Bindable]
		static public  var fontSize:ArrayCollection=new ArrayCollection([
			{label:9, data:2}, 
			{label:10,data:3},
			{label:12,data:4},
			{label:16,data:5},
			{label:18,data:6},
			{label:20,data:7},
			{label:25,data:8},
			{label:30,data:9},
			{label:33,data:10},
			{label:35,data:11},
			{label:40,data:12},
			{label:45,data:13},
			{label:50,data:14},
			{label:60,data:15},
			{label:70,data:16}
		]);

		[Bindable]
		static public var selectBooktypeOne:ArrayCollection=new ArrayCollection([
			{ingredient:"小说文学", category:"小说文学"}, 
			{ingredient:"娱乐时尚", category:"娱乐时尚"}, 
			{ingredient:"校园青春", category:"校园青春"}, 
			{ingredient:"励志/成功", category:"励志/成功"}, 
			{ingredient:"少儿", category:"少儿"}, 
			{ingredient:"人文历史", category:"人文历史"},
			{ingredient:"体育健康", category:"体育健康"},
			{ingredient:"家庭美食", category:"家庭美食"},
			{ingredient:"地理旅游", category:"地理旅游"},
			{ingredient:"国学古典", category:"国学古典"},
			{ingredient:"期刊杂志", category:"期刊杂志"},
			{ingredient:"武侠奇幻", category:"武侠奇幻"},
			{ingredient:"经济管理", category:"经济管理"},
			{ingredient:"政治经济", category:"政治经济"},
			{ingredient:"婚恋与性", category:"婚恋与性"},
			{ingredient:"专业学科", category:"专业学科"},
			{ingredient:"人物传记", category:"人物传记"}
		]);
		
		/**
		 * 获取选择的区域
		 * 
		 * */
		public function seleteRange(event:SelectionEvent):void{
			var range:ElementRange=event.selectionState ?
				ElementRange.createElementRange(event.selectionState.textFlow,
					event.selectionState.absoluteStart,event.selectionState.absoluteEnd) : null;
			if(range!=null){
				textFlow=range.textFlow;
			}
		}
		/**
		 *  改变选择区域字体颜色的值
		 * 
		 * */
		public function color_change(event:ColorPickerEvent):void{
			colorValue=event.target.selectedColor;
			if(textFlow != null){
				var editManage:IEditManager=textFlow.interactionManager as IEditManager;
				//设置颜色
				var textLayoutFormat:TextLayoutFormat=new TextLayoutFormat();
				textLayoutFormat.color=colorValue;
				//更新	
				editManage.applyLeafFormat(textLayoutFormat);
			}
		}
		/**
		 * 改变字体的大小
		 * 
		 * */
		public function fontSize_change(event:DropdownEvent):void{
			fontSizeValue=event.target.selectedItem.label;
			if(textFlow != null){
				var editManage:IEditManager=textFlow.interactionManager as IEditManager;
				//设置A大小
				var textLayoutFormat:TextLayoutFormat=new TextLayoutFormat();
				textLayoutFormat.fontSize=fontSizeValue;
				//更新	
				editManage.applyLeafFormat(textLayoutFormat);
			}
		}
		/**
		 * 获取更改的字体的格式
		 * 
		 * */
		public function fontFamily_change(event:DropdownEvent):void{
			fontFamilyValue=event.target.selectedItem.label;
			
			if(textFlow != null){
				var editManage:IEditManager=textFlow.interactionManager as IEditManager;
				//设置样式
				var textLayoutFormat:TextLayoutFormat=new TextLayoutFormat();
				textLayoutFormat.fontFamily=fontFamilyValue;
				//更新	
				editManage.applyLeafFormat(textLayoutFormat);
			}
		}
		/**
		 * 把当前选择的字体设置为粗体
		 * 如果当前的字体已经是粗体了就
		 * 设置为标准
		 * */
		public function fontWeight_change(event:MouseEvent):void{
			if(textFlow != null){
				var editManage:EditManager=textFlow.interactionManager as EditManager;
				//设置粗细
				var textLayoutFormat:TextLayoutFormat=new TextLayoutFormat();
				if(editManage.getCommonCharacterFormat().fontWeight==FontWeight.BOLD){
					textLayoutFormat.fontWeight=FontWeight.NORMAL;
				}else{
					textLayoutFormat.fontWeight=FontWeight.BOLD;
				}
				//更新	
				editManage.applyLeafFormat(textLayoutFormat);
			}
		}
		/**
		 * 把当前选择的字体更改为斜体
		 * 如果当前的字体已经为斜体就
		 * 把当前选择的字体改为标准字体
		 * */
		public function italic_change(event:MouseEvent):void{
			if(textFlow!=null){
				var editManage:IEditManager=textFlow.interactionManager as IEditManager;
				
				var textLayoutFormat:TextLayoutFormat=new TextLayoutFormat();
				if(editManage.getCommonCharacterFormat().fontStyle==FontPosture.ITALIC){
					textLayoutFormat.fontStyle=FontPosture.NORMAL;
				}else{
					textLayoutFormat.fontStyle=FontPosture.ITALIC;
				}
				
				editManage.applyLeafFormat(textLayoutFormat);
			}
		}
		/**
		 * 文本居中，靠左，靠右
		 * 显示设置
		 * */
		public function textAlign_change(event:MouseEvent):void{
			if(textFlow!=null){
				var editManage:IEditManager=textFlow.interactionManager as IEditManager;
				
				var textLayoutFormat:TextLayoutFormat=new TextLayoutFormat();
				if(String(event.currentTarget.id)=="centerChange"){
					textLayoutFormat.textAlign=TextAlign.CENTER;
				}else if(String(event.currentTarget.id)=="justifyChange"){
					textLayoutFormat.textAlign=TextAlign.JUSTIFY;
				}else if(String(event.currentTarget.id)=="leftChange"){
					textLayoutFormat.textAlign=TextAlign.LEFT;
				}else if(String(event.currentTarget.id)=="rightChange"){
					textLayoutFormat.textAlign=TextAlign.RIGHT;
				}
				IEditManager(textFlow.interactionManager).applyParagraphFormat(textLayoutFormat);
				textFlow.interactionManager.setFocus();
			}

		}
		/**
		 * 下划线的设置
		 * 
		 * */
		public function underLine_change(event:MouseEvent):void{
			if(textFlow!=null){
				var editManage:IEditManager=textFlow.interactionManager as IEditManager;
				
				var textLayoutFormat:TextLayoutFormat=new TextLayoutFormat();
				if(editManage.getCommonCharacterFormat().textDecoration==TextDecoration.UNDERLINE){
					textLayoutFormat.textDecoration=TextDecoration.NONE;
				}else{
					textLayoutFormat.textDecoration=TextDecoration.UNDERLINE;
				}
				
				editManage.applyLeafFormat(textLayoutFormat);
			}
		}
		public function font_bg_change(event:ColorPickerEvent):void{
			if(textFlow!=null){
				var editManage:IEditManager=textFlow.interactionManager as IEditManager;
				
				var textLayoutFormat:TextLayoutFormat=new TextLayoutFormat();
				textLayoutFormat.backgroundColor=event.target.selectedColor;
				
				editManage.applyLeafFormat(textLayoutFormat);
			}
		}
		/**
		 * 前一行和后一行之间的距离
		 * 
		 * */
		public var _height:Number=1;
		public function line_width(event:MouseEvent):void{
			if(textFlow!=null){
				var editManage:IEditManager=textFlow.interactionManager as IEditManager;
				if(event.currentTarget.id=="add_width"){
					_height++;
				}else{
					_height--;
				}
				var textLayoutFormat:TextLayoutFormat=new TextLayoutFormat();
				textLayoutFormat.trackingRight=_height;
				
				
				editManage.applyLeafFormat(textLayoutFormat);
			}
		}
	}
}